package com.atguigu.bigdata.chapter11.window;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

/**
 * @Author lzc
 * @Date 2022/9/9 14:09
 */
public class Flink05_TVF_1 {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        
        
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        
        tEnv.executeSql("create table sensor(" +
                            "   id string, " +
                            "   ts bigint, " +
                            "   vc int, " +
                            "   et as TO_TIMESTAMP_LTZ(ts, 3),  " +// 添加一个时间戳字段, 能否直接认为他就是事件时间?  必能, 必须添加水印
                            "   watermark for et as et - interval '3' second " + // 添加水印
                            ")with(" +
                            "   'connector' = 'filesystem', " +
                            "   'path' = 'input/sensor.txt', " +
                            "   'format' = 'csv' " +
                            ")");
        
       /* tEnv
            .sqlQuery("select " +
                          " id, window_start, window_end, " +
                          " sum(vc) " +
                          "from table( tumble(table sensor, descriptor(et), interval '5' second) )" +
                          "group by id, window_start, window_end")  // 分组的时候: window_start, window_end 至少添加一个
            .execute()
            .print();*/
    
    
        tEnv
            .sqlQuery("select " +
                          " id, window_start, window_end, " +
                          " sum(vc) " +
                            // tvf 的滑动窗口: 限制窗口长度必须是滑动步长的整数倍
                          "from table( hop(table sensor, descriptor(et), interval '2' second, interval '6' second) )" +
                          "group by id, window_start, window_end")  // 分组的时候: window_start, window_end 至少添加一个
            .execute()
            .print();
        
    }
}
/*
 
 
 */
